package com.ctrl.riveradministrator.mvp.view.activity.work_bench;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;

/**
 * Created by lizhunan on 2020/4/8
 */
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.Holder> {

    private Context context;

    private String[] titles;
    private int[] icons, iconsBack;
    private int[] colors;
    private GridClickListener clickListener;

    public GridAdapter(Context context, String[] titles, int[] icons, int[] iconsBack, int[] colors) {
        this.context = context;
        this.titles = titles;
        this.icons = icons;
        this.iconsBack = iconsBack;
        this.colors = colors;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.other_item, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        holder.title.setText(titles[position]);
        holder.v.setBackgroundResource(colors[position]);
        holder.icon.setImageResource(icons[position]);
        holder.iconBack.setImageResource(iconsBack[position]);
        holder.content.setOnClickListener(v -> clickListener.onGridItemClick(position));
    }

    @Override
    public int getItemCount() {
        return titles.length;
    }

    public void setClickListener(GridClickListener clickListener) {
        this.clickListener = clickListener;
    }

    interface GridClickListener {
        void onGridItemClick(int position);
    }

    class Holder extends RecyclerView.ViewHolder {

        private TextView title;
        private View v;
        private ImageView icon, iconBack;
        private CardView content;

        Holder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            v = itemView.findViewById(R.id.v);
            icon = itemView.findViewById(R.id.icon);
            iconBack = itemView.findViewById(R.id.icon_back);
            content = itemView.findViewById(R.id.content);
        }
    }
}
